import os
from dotenv import load_dotenv
from mt4_trader import MT4Trader
from chart_analyzer import ChartAnalyzer
from config import Config
from mt4_client import MT4Client
from strategy_controller import StrategyController
import logging
import time
from chart_capturer import ChartCapturer

# 配置日志
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler('trading.log'),
        logging.StreamHandler()
    ]
)

def main():
    # 初始化
    chart_capturer = ChartCapturer()
    chart_analyzer = ChartAnalyzer(chart_capturer)
    
    # 要监控的货币对列表
    symbols = ["EURUSD", "GBPUSD", "USDJPY", "USDCHF"]
    current_symbol_index = 0
    
    try:
        while True:
            current_symbol = symbols[current_symbol_index]
            
            # 切换到下一个图表
            if chart_capturer.switch_chart(current_symbol):
                # 等待5秒让图表稳定
                time.sleep(5)
                
                # 捕获并分析图表
                image = chart_capturer.capture_chart(current_symbol, "H1", save_debug=True)
                if image is not None:
                    # 传入上次切换时间进行分析
                    signal_type, info = chart_analyzer.analyze_chart(
                        image, 
                        current_symbol, 
                        "H1", 
                        save_debug=True,
                        last_switch_time=chart_capturer.last_switch_time
                    )
                    
                    if signal_type:
                        print(f"\n检测到信号:")
                        print(f"货币对: {current_symbol}")
                        print(f"信号类型: {signal_type}")
                        print(f"信号信息: {info}")
                
            # 更新索引到下一个货币对
            current_symbol_index = (current_symbol_index + 1) % len(symbols)
            
            # 等待5秒再切换到下一个图表
            time.sleep(5)
            
    except KeyboardInterrupt:
        print("\n程序已停止")
    except Exception as e:
        print(f"\n程序出错: {str(e)}")

if __name__ == "__main__":
    main() 